//模版管理-模版列表
App.util.ns('Admin.Template.Message');
App.pages.add((function(){
    var NS = Admin.Template.Message;
    var page = new App.Page({
        name: 'template/message/index'
    });
    //页面初始化
    page.initStack.add('dataTable', function(){
        //面包屑导航
        this.setBreadcrumb([{
            icon: '<i class="fa fa-volume-up"></i>',
            content: '模版消息管理'
        }, {
            content: '消息列表'
        }]);
        this.dataTable = new App.DataTable({
            baseUrl: '/admin/template/message',
            dataUrl: 'lists',
            table: {
                id: 'id',
                main: 0,
                showId: false,
                columns: App.util.copy(NS.Conf.columns)
            },
            filter: [
                {type: 'keywords'},
                {type: 'timeRange'},
                {type: 'select', name: 'type', title: '模版类型', width: '90px', items: [
                    [0, '商品通知'],
                ]},
                {type: 'select', name: 'status', title: '状态', width: '90px', items: [
                    [0, '未发送'],
                    [1, '已发送']
                ]}
            ],
            menu: [
                {parent: 'add', permission: 'op:admin.template.message.create'},
                {type: 'globe', name: '推送所有用户', permission: 'op:admin.template.message', operation:function (dataTable) {
                    var temp_ids = dataTable.table.getSelectedIds();
                    if(temp_ids.length == 1){
                        $.ajax({
                            url: 'admin/template/message/message',
                            data:{message_id: temp_ids[0]},
                            type: "get",
                            async: true,
                            success: function (returnData){
                                if(returnData){
                                    App.util.hint.toast({
                                        type: 'success',
                                        text: '推送成功'
                                    }).delay(500);
                                }
                            },
                            error: function () {
                                App.util.hint.toast({
                                    type: 'error',
                                    text: '格式不对'
                                }).delay(500);
                            }
                        });
                    }else{
                        App.util.hint.alert({
                            text: '必须选中一条消息'
                        });
                    }
                }},
                {parent: 'view', permission: 'op:admin.template.message.detail'},
                {type: 'inline', permission: 'op:admin.template.message',conditions: [['status', '0']], name: '选择用户', operation: function(dataTable,tempmessage){
                    var win = dataTable.wins.get('sendMessage');
                    win.setTitle('批量选择用户');
                    win.show();
                    win.dataTable.table.frame.find('.chk-row').prop('checked', false).change();
                    //设置按钮
                    win.setBtns([(function(){
                        var cancelBtn = $('<a class="btn btn-danger btn-xs m-right-sm">取消</a>');
                        cancelBtn.click(function(){
                            win.hide();
                        });
                        return cancelBtn;
                    })(), (function(){
                        var saveBtn = $('<a class="btn btn-success btn-xs">保存</a>');
                        saveBtn.click(function(){
                            var userIds = win.dataTable.table.getSelectedIds();
                            var messageId = tempmessage.id;
                            if(userIds.length){
                                dataTable.postData({
                                    url: 'selectuser',
                                    alert: '保存成功！',
                                    reload: true,
                                    data: {
                                        message_id: messageId,
                                        user_ids: userIds.join(',')
                                    },
                                    after: function(){
                                        win.hide();
                                    }
                                });
                            }else{
                                App.util.hint.alert({
                                    text: '没有选择用户'
                                });
                            }
                        });
                        return saveBtn;
                    })()]);
                }},
                {type: 'inline', permission: 'op:admin.template.message.sendMessage',
                    operation: {type: 'change', url: 'sendMessage'},
                    name: '推送选择用户',conditions: [['status', '0']]
                },
                {parent: 'edit', permission: 'op:admin.template.message.update',conditions: [['status', '0']]},
                {parent: 'drop', permission: 'op:admin.template.message.delete'},
                {parent: 'multiDrop', permission: 'op:admin.template.message.multiDelete'},
                {parent: 'oplog', permission: 'op:admin.template.message.getOpLogs'}
            ],
            detail: {
                form: {
                    items: App.util.copy(NS.Conf.form)
                }
            },
            after: function(dataTable){
                dataTable.wins.add(new NS.SendMessage(dataTable), 'sendMessage');
            },
        }, page);
        this.dataTable.fix(this.contentContainer);
        this.dataTable.loadData();
    });
    //页面刷新
    page.refreshStack.add('dataTable', function(){
        this.dataTable.loadData(true);
    });
    //页面销毁
    page.destroyStack.add('dataTable', function(){
        this.dataTable.destroy();
    });
    return page;
})());